package com.cyn.service.impl;

import com.cyn.dao.ProjectMapper;
import com.cyn.dao.SuccessKilledMapper;
import com.cyn.entity.Project;
import com.cyn.entity.SuccessKilled;
import com.cyn.service.RedisService;
import com.cyn.service.SuccessKilledService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SuccessKilledServiceImpl implements SuccessKilledService {

    @Autowired
    SuccessKilledMapper successKilledMapper;

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    RedisService redisService;

    @Override
    public SuccessKilled insert(SuccessKilled successKilled) {
        int id = successKilledMapper.insert(successKilled);
        return successKilled;
    }

    @Override
    public SuccessKilled get(Long id) {
        return successKilledMapper.get(id);
    }

    @Override
    public List<SuccessKilled> getList() {
        return successKilledMapper.getList(new SuccessKilled());
    }

    @Override
    public Integer getListCount() {
        return null;
    }

    @Override
    public Map<String, Object> skilledByRedisLock(String userPhone, String projectId,String index) {
        Map<String,Object> params = new HashMap<>();

        Project project = new Project();
        project.setId(Long.parseLong(projectId));
        Map<String,Object> result = jkc2(project,params,userPhone,true, index);
        return result;
    }

    private Map<String, Object> jkc2(Project project,Map<String,Object> params,String userPhone, boolean lock,String index) {
        lock = redisService.getLock();
        project = projectMapper.get(project.getId());
        if(lock){
            if(Integer.parseInt(project.getStock())<=0){
                params.put("code",-1);
                params.put("msg","库存不足");
                SuccessKilled successKilled = new SuccessKilled();
                successKilled.setProjectId(project.getId());
                successKilled.setUserPhone(userPhone);
                successKilled.setState(1);
                String[] tmp = index.split("_");
                successKilled.setIindex(Integer.parseInt(tmp[0]));
                successKilled.setJindex(Integer.parseInt(tmp[1]));
                successKilledMapper.insert(successKilled);
                redisService.unLock();
                return params;
            }
            params.put("code", 1);
            params.put("msg", "秒杀成功");
            SuccessKilled successKilled = new SuccessKilled();
            successKilled.setProjectId(project.getId());
            successKilled.setUserPhone(userPhone);
            successKilled.setState(0);
            String[] tmp = index.split("_");
            successKilled.setIindex(Integer.parseInt(tmp[0]));
            successKilled.setJindex(Integer.parseInt(tmp[1]));
            successKilledMapper.insert(successKilled);
            Integer stock = Integer.parseInt(project.getStock())-1;
            project.setStock(String.valueOf(stock));
            projectMapper.update(project);
            redisService.unLock();
            return params;
        }else{
            return jkc2(project,params,userPhone,true,index);
        }
    }

    @Override
    public Map<String, Object> skilled(String userPhone, String projectId) {
        Map<String,Object> params = new HashMap<>();
        Project project = new Project();
        project.setId(Long.parseLong(projectId));
        Map<String,Object> result = jkuc(project,params,userPhone);
        return result;
    }

    private Map<String,Object> jkuc(Project project,Map<String,Object> params,String userPhone) {
        project = projectMapper.get(project.getId());

        Integer stock = Integer.parseInt(project.getStock())-1;
        project.setStock(String.valueOf(stock));
        Integer version = Integer.parseInt(project.getVersion())+1;
        int result = projectMapper.updateVersion(project);
        if(result>=1) {
            params.put("code", 1);
            params.put("msg", "秒杀成功");
            SuccessKilled successKilled = new SuccessKilled();
            successKilled.setProjectId(project.getId());
            successKilled.setUserPhone(userPhone);
            successKilled.setState(0);
            successKilledMapper.insert(successKilled);
            return params;
        }else{
            if(Integer.parseInt(project.getStock())<=0){
                params.put("code",-1);
                params.put("msg","库存不足");
                SuccessKilled successKilled = new SuccessKilled();
                successKilled.setProjectId(project.getId());
                successKilled.setUserPhone(userPhone);
                successKilled.setState(1);
                successKilledMapper.insert(successKilled);
                return params;
            }else {
                project.setVersion(String.valueOf(version));
                return jkuc(project, params,userPhone);
            }
        }
    }
}
